<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of dmdforms_controller
 *
 * @author Youness.MONDIR-SC
 */
class DmdformsController extends AppController {
    var $name='Dmdforms';
    var $msg=null; var $isValid=array();

    function  beforeFilter() {
            parent::beforeFilter();
            $this->Auth->autoRedirect = false;
            switch ($this->Auth->user('group_id')){
                case 1:$this->Auth->allow('*');break;
                case 2:$this->Auth->deny('*');$this->Auth->allow('*');break;
                case 3:$this->Auth->deny('*');$this->Auth->allow($this->eicoArray);break;
                case 4:$this->Auth->deny('*');$this->Auth->allow($this->toolAdminArray);break;
                case 5:$this->Auth->deny('*');$this->Auth->allow($this->demanderArray);break;
                case 7:$this->Auth->deny('*');$this->Auth->allow($this->basicUserArray);break;
                case 6:$this->Auth->deny('*');$this->Auth->allow($this->requesterArray);break;
            }
            $this->set('active',2);
        }

    function listActions(){
        $cont=array(
          'Dmdforms','Actions','Actiontypes','Companies','Countries','Dmdformstatuses',
          'Dmdformtypes','Formstatuses','Groups','Jobfunctions','Jobprofiles','Kpis',
          'Modules','Powerdelegations','Securitygroups','Statuses','Tools','Users','Workflows'
        );
        foreach ($cont as $c){
            $classShortName = $c;
            $className = $classShortName.'Controller';
            App::import('Controller',$classShortName);
            $rc = new ReflectionClass($className);
            $methods = $rc->getMethods();
            $actions = array();
            foreach ($methods as $m){
                if ($m->isPublic() && $m->getDeclaringClass()->name ==  $className){
                    $actions[] = $m->name;
                }
            }
            $a[$c]=$actions;
        }
        $this->set('e',$a);
    }

    //Creer un nouveau utilisateur dans la demande
    function editNewUser($formId=null,$save=true){
        if(isset($this->data['Dmdform']['user_name'])):
        if(strlen($this->data['Dmdform']['user_name'])>0){$this->data['Dmdform']['is_new_user']=1;}endif;
        if($save){$this->saveData($formId,$this->data);}
        $formData['statuses']= $this->Dmdform->User->Status->find('list');
        $users=$this->Dmdform->getUsers();
        $formData['users']=$users;
        $formData['hrreferents']= $this->Dmdform->User->find('list',array('fields'=>array('strName')));
        $this->set('formData',$formData);return $formData;
    }
    //Creer un nouveau utilisateur dans la demande
    function editUser($formId=null,$save=true){
        if(isset($this->data['Dmdform']['user_name'])):
        if(strlen($this->data['Dmdform']['user_name'])>0){$this->data['Dmdform']['is_new_user']=1;}endif;
        if($save){$this->saveData($formId,$this->data);}
        $formData['statuses']= $this->Dmdform->User->Status->find('list');
        $formData['hrreferents']= $this->Dmdform->User->find('list',array('fields'=>array('strName')));
        $this->set('formData',$formData);return $formData;
    }

    //Ajouter Company
    function editNewCompany($formId=null,$save=true){
        if(isset($this->data['Dmdform']['company_name'])) {$this->data['Dmdform']['is_new_company']=1;}
        if($save){$this->saveData($formId,$this->data);};return array();
    }

    //Ajouter Job
    function editJob($formId=null,$save=true){
        if($save){$this->saveData($formId,$this->data);}
        $formData['joblists']=$this->Dmdform->getJobListes();
        $formData['prs']= $this->Dmdform->User->find('list',array('fields'=>array('strName')));
        $this->set('formData',  $formData);return $formData;
    }
    //Ajouter branches et cost centers
    function editBranch($formId=null,$save=true){
        if($save){$this->saveData($formId,$this->data);}
        if(isset($this->data['Dmdform']['user_branch_ids'])){
            $data=$this->data['Dmdform'];
            $this->data['Dmdform']['user_branch_ids']=serialize($data['user_branch_ids']);
            $this->data['Dmdform']['user_costcenter_ids']=serialize($data['user_costcenter_ids']);
        }
        $formData['joblists']=$this->Dmdform->getJobListes();
        $formData['prs']= $this->Dmdform->User->find('list',array('fields'=>array('strName')));
        $this->set('formData',  $formData);return $formData;
    }

    //Ajouter Workflow
    function editWorkflow($formId=null,$save=true){
        $data=$this->data['Dmdform'];$wkids=null;$pdids=null;
        for($i=0;$i<10;$i++){
            if (!empty($data['user_workflow_id'.$i])){
                $wkids.=$data['user_workflow_id'.$i].";";
                if (!empty($data['user_powerdelegation_id'.$i])){
                    $pdids.=$data['user_powerdelegation_id'.$i].';';
                }else{
                    $pdids.='0;';
                }
            }
        }
        $data=array();
        $this->data['Dmdform']['user_workflow_id']=$wkids;
        $this->data['Dmdform']['user_powerdelegation_id']=$pdids;
        
        if($save){$this->saveData($formId,$this->data);}
        $formData['workflows']=$this->Dmdform->getWkListe();
        $this->set('formData',  $formData);return $formData;
    }

    //ajouter Backup
    function editNewBackup($formId=null,$save=true){
        $this->data['Dmdform']['is_new_backup']=1;
        if($save){$this->saveData($formId,$this->data);}
        $formData['joblists']=  $this->Dmdform->getJobListes();
        $formData['dmdforms']=  $this->Dmdform->find('list');
        $this->set('formData',$formData);return $formData;
    }

    //ajouter Manager
    function editNewManager($formId=null,$save=true){
        $this->data['Dmdform']['is_new_manager']=1;
        if($save){$this->saveData($formId,$this->data);}
        $formData['dmdforms']=  $this->Dmdform->find('list');
        $this->set('formData',$formData);return $formData;
    }
    
    function managerValidation($formId=null,$save=true){
        $this->data['Dmdform']['manager_visa_date']=date('Y-m-d H:i:s');
        if($save){$this->saveData($formId,$this->data);}
        
        $formData['dmdforms']=  $this->Dmdform->find('list');
        $this->set('formData',$formData);return $formData;
    }

    //ajouter user existant
    function addUser($formId=null,$save=true){
        if($this->data && $formId){
            $this->data['Dmdform']['is_new_user']=0;                  
            if($save){$this->saveData($formId,$this->data);}
        }
            $users=$this->Dmdform->getUsers();
            $formData['users']=$users;
            return $formData;
            $this->set('formDtat',$formData);

    }

    //ajouter user existant
    function addBackup($formId=null,$save=true){
        if($this->data && $formId){
            $this->data['Dmdform']['is_new_backup']=0;
            if($save){$this->saveData($formId,$this->data);}
        }
            $users=$this->Dmdform->getUsers();
            $formData['users']=$users;
            return $formData;
            $this->set('formDtat',$formData);

    }

    //ajouter user existant
    function addManager($formId=null,$save=true){
        if($this->data && $formId){
            $this->data['Dmdform']['is_new_manager']=0;
            if($save){$this->saveData($formId,$this->data);}
        }
            $users=$this->Dmdform->getUsers();
            $formData['users']=$users;
            return $formData;
            $this->set('formDtat',$formData);

    }

    //ajouter user existant
    function addProfileReference($formId=null,$save=true){
        if($this->data && $formId){            
            $this->data['Dmdform']['is_new_profile_reference']=0;
            if($save){$this->saveData($formId,$this->data);}
        }
            $users=$this->Dmdform->getUsers();
            $formData['users']=$users;
            return $formData;
            $this->set('formDtat',$formData);

    }

    //ajouter Company
    function addCompany($formId=null,$save=true){
        $this->data['Dmdform']['is_new_company']=0;
       // $this->data['Dmdform']['company']=0;
        if($save){$this->saveData($formId,$this->data);}
        $formData['companies']=  $this->Dmdform->getCompanies();
        $this->set('formData',$formData); return $formData;
    }

    //ajouter pc
    function addPc($formId=null,$save=true){
        if($save){$this->saveData($formId,$this->data);}
        return array();
    }

    // ajouter outil
    function addTools($formId=null,$save=true){
        $formData['tools']=$this->Dmdform->getTools();
        $formData['toolsTable']=$this->Dmdform->getToolsTables();
        $toolAdmin=array();
        $data=$this->data['Dmdform'];$tools=null;
        for($i=0;$i<count($formData['tools']);$i++){
            if (!empty($data['tool_id'.$i])){
                $tools.=$data['tool_id'.$i].";";
                $toolAdmin[$data['tool_id'.$i]]=0;
                //debug($data);
                if(!empty($data['tool_table'.$i])){
                    if(!empty($data['toolTableName'.$i])){
                        $t=$data['toolTableName'.$i];
                        //debug($this->data);
                        $dt=ucfirst(substr($t, 0,  strlen($t)-1));
                        //debug($dt);
                        $this->Dmdform->User->Powerdelegation->Workflow->Tool->$dt->save($this->data[$dt.'s']);
                    }
                }
            }
        }
        $data=array();
        $this->data['Dmdform']['tool_ids']=$tools;
        $this->data['Dmdform']['tool_admin']=serialize($toolAdmin);
        if($save){$this->saveData($formId,$this->data);}
        $this->set('formData',$formData);return $formData;
    }

    //ajouter notes et date d'execution
    function addNotesAndExecutionDate($formId=null,$save=true){
        if($save){$this->saveData($formId,$this->data);}
        return array();
    }

    //save form module data to dmdform
    function saveData($formId=null,$data=null){

        if (!empty($this->data)){
            if(!empty($formId)){
                $this->Dmdform->id=$formId;
                $this->Dmdform->save($this->data);
            }
        }else{
            if(!empty($formId)){
                $this->Dmdform->id=$formId;
                $this->data=$this->Dmdform->read();
                $this->set($this->data);
            }
        }
    }

    //Construct form
    function newDmdform($modifyForm=null,$id=null){
        $formData=array();
        //debug($this->Dmdform->invalidFields());
        //debug($this->data);
        //if(empty($this->data) && !empty($data)){$this->data=$data;}
        if (!empty($modifyForm) && !empty($id) && $modifyForm==1){
            $this->data=unserialize($this->Dmdform->getFormData($id));
            $this->set('modifyForm',$modifyForm);
        }
        if ($this->data){
            if(isset($this->data['Dmdform']['dmdformtype_id'])):
            $modules=$this->Dmdform->getModules($this->data['Dmdform']['dmdformtype_id']);
            $formData['modules']=  $modules;
            $formData['moduleNames']= $this->Dmdform->getModuleMenuNames($this->data['Dmdform']['dmdformtype_id']);
            $formData['type']= $this->data['Dmdform']['dmdformtype_id'];endif;
            foreach ($modules as $m):
                $formData=array_merge($formData,$this->$m());
            endforeach;
            $this->set('formData',$formData);
            $this->set('NewDmd',false);
            //debug($this->msg);
            $this->set('errors',$this->msg);
        }else{
            $this->set('types',$this->Dmdform->Dmdformtype->find('list'));
            $this->set('NewDmd',true);
        }
        
    }
    
    //mise a jour du formulaire
    function updateDmdform(){
        if ($this->data){
             if($this->validateData($this->data['Dmdform']['dmdformtype_id'])){
                $typeId=$this->data['Dmdform']['dmdformtype_id'];
                $formId= $this->data['Dmdform']['id'];
                $modules=$this->Dmdform->getModules($typeId);
                foreach ($modules as $m):
                    $this->$m($formId,false);
                endforeach;
                
                $this->data['Dmdform']['formdata']=serialize($this->data);
                
                if($this->Dmdform->save($this->data)){
                    $this->Session->setFlash(__('form modified !',true));
                }else{
                    $this->Session->setFlash(__('form not modified, try again please !',true));
                    $this->Sendmail->newMail(8,$this->data['Dmdform']['code'],$formId, $this->Dmdform->Dmdformtype->getName($typeId));
                }                
                 $this->redirect(array('controller'=>'formstatuses','action'=>'showForms'));
             }else{
                $this->Session->setFlash( __('Error',true) ,'flash_success');
                $this->newDmdform($this->data);
                $this->render('newDmdform');

            }
        }
    }

    //enregistrer le formulaire
    function saveDmdform(){
        $typeId=null;$formId=null;
        $this->set('data',  $this->data);
        if ($this->data){
            //$this->Dmdform->set($this->data);
            if($this->validateData($this->data['Dmdform']['dmdformtype_id'])){
if($this->data['Dmdform']['dmdformtype_id']!=1){
                    if(isset($this->data['Dmdform']['user_id']) && !empty($this->data['Dmdform']['user_id'])){
                        $uid=$this->data['Dmdform']['user_id'];
                        $comp=$this->Dmdform->User->find('list',array('conditions'=>array('id'=>$uid),'fields'=>array('company_id')));
                        $this->data['Dmdform']['company_id']=$comp[$uid];
                    }
                }
                $typeId=$this->data['Dmdform']['dmdformtype_id'];
                $formId= $this->_createDmdform($typeId);
                $modules=$this->Dmdform->getModules($typeId);
                foreach ($modules as $m):
                    $this->$m($formId,false);
                endforeach;
                if(isset($this->data['Dmdform']['user_id']) && !empty($this->data['Dmdform']['user_id'])){
                    $uid=$this->data['Dmdform']['user_id'];
                }else{
                    $uid=null;
                }
                $this->_createCode($typeId, $formId, $uid);
                $this->_createFormStatus($formId, $typeId);
                $this->Dmdform->id=$formId;
                $this->data['Dmdform']['id']=$formId;
                $this->data['Dmdform']['demander_id']=$this->Auth->user('id');
                if(isset($this->data['Dmdform']['manager_id']) && !empty($this->data['Dmdform']['manager_id'])){
                    $mid=$this->data['Dmdform']['manager_id'];
                    if($mid==$this->Auth->user('id')){
                        $dmder='ismanager';
                    }else{
                        $dmder='isHR';
                    }
                }
                $this->data['Dmdform']['formdata']=serialize($this->data);
                if($this->Dmdform->save($this->data)){
                    $this->Session->setFlash(__('form created !',true));
                    $this->Sendmail->newMail(1,$this->data['Dmdform']['code'],$formId,  $this->Dmdform->Dmdformtype->getName($typeId));
                }else{
                    $this->Session->setFlash(__('form not created, try again please !',true));
                }
                $this->redirect(array('controller'=>'formstatuses','action'=>'showForms'));
            }else{
                $this->Session->setFlash( __('Error',true) ,'flash_success');
                $this->newDmdform($this->data);
                $this->render('newDmdform');
            }
        }
        //debug($this->data);
    }

    //validation des donnes
    function validateData($typeId){
        $nbV=0;
        $modules=$this->Dmdform->getModulesValidation($typeId);
                foreach ($modules as $m):
                    if (isset($m) && !empty($m)){
                    $this->isValid[$nbV]=$this->$m();
                    $nbV++;}
                endforeach;
        
        
        if (in_array(false, $this->isValid)){
            return false;
        }else{
            return true;
        }

    }

    //creer le code du formulaire + ajout de la date
    function _createCode($typeId,$formId,$userId=null){
        if($userId){$uname=$this->Dmdform->getUser($userId);}else{$uname=substr(ucfirst($this->data['Dmdform']['user_surname']),0,1).".".strtoupper($this->data['Dmdform']['user_name']);}
        $this->data['Dmdform']['code']= $uname."-".$this->Dmdform->getType($typeId)."-".date('m-y')."-". substr("0000".($formId),-4);
        $this->data['Dmdform']['form_date']=date('Y-m-d H:i:s');

        return $this->data['Dmdform']['code'];
    }

    //cree une nouvelle entree formstatus
    function _createFormStatus($formId,$typeId){
        $dataForm['Formstatus']['dmdforms_dmdformtype_id']=$typeId;
        $dataForm['Formstatus']['dmdformstatus_id']=1;
        $dataForm['Formstatus']['dmdform_id']=$formId;
        if(isset($this->data['Dmdform']['user_id']) && !empty($this->data['Dmdform']['user_id'])){
                $uid=$this->data['Dmdform']['user_id'];
            }else{
                $uid=0;
            }
        $dataForm['Formstatus']['demander_id']=$this->Auth->user('id');
        
        $dataForm['Formstatus']['user_id']=$uid;
        $this->Dmdform->Formstatus->save($dataForm);
    }

    //crrer une nouvelle entree
    function _createDmdform($typeId=null){
        if ($typeId):
            $this->Dmdform->create();
            $this->Dmdform->saveField('dmdformtype_id',$typeId);
            return $this->Dmdform->id;
         endif;
        }
        
    //valider les donnee user
    function _validateUser(){
        $data=$this->data['Dmdform'];
        if (empty($data['user_id']) && empty($data['user_name'])){
            
            $this->msg.= __('You must specify a user <br/>',true) ;
            return false;
        }
        if (empty($data['user_id']) && !empty($data['user_name'])){
            $validation=array(
                'username'=>array(
                    'data'=>$data['user_name'],
                    'rules'=>array('notEmpty','alphaNumeric'),
                    'fieldName'=>'User Name'
                ),
                'usersurname'=>array(
                    'data'=>$data['user_surname'],
                    'rules'=>array('notEmpty','alphaNumeric'),
                    'fieldName'=>'User Surname'
                ),
                'status'=>array(
                    'data'=>$data['user_status_id'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'User status'
                ),
                'hrreferent'=>array(
                    'data'=>$data['user_hrreferent_id'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'User Hr Referent'
                ),
            );
            if ($this->Validform->validateArray($validation)){
                return true;
            }else{
                $this->msg.=$this->Validform->getMsg();
                return false;
            }
        }
        if(!empty($data['user_id'])){ return true;}
    }
    function _validateCompany(){
        $data=$this->data['Dmdform'];
        
        
            $validation=array(
                'country'=>array(
                    'data'=>$data['company_country'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Company Country'
                ),
                'company'=>array(
                    'data'=>$data['company_title'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Company name'
                ),
                'department'=>array(
                    'data'=>$data['company_department'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Company department'
                ),
                'site'=>array(
                    'data'=>$data['company_site'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Company site'
                ),
              
            );
            if ($this->Validform->validateArray($validation)){
                return true;
            }else{
                $this->msg.=$this->Validform->getMsg();
                return false;
            }
      
    }
    function _validateManager(){
        $data=$this->data['Dmdform'];
                
            $validation=array(
                'manager'=>array(
                    'data'=>$data['manager_id'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Manager'
                )
              
            );
            if ($this->Validform->validateArray($validation)){
                return true;
            }else{
                $this->msg.=$this->Validform->getMsg();
                return false;
            }
      
    }
    function _validateBackup(){
        $data=$this->data['Dmdform'];
                
            $validation=array(
                'backup'=>array(
                    'data'=>$data['backup_id'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Back Up'
                )
              
            );
            if ($this->Validform->validateArray($validation)){
                return true;
            }else{
                $this->msg.=$this->Validform->getMsg();
                return false;
            }
      
    }
    function _validateJobs(){
        $data=$this->data['Dmdform'];
        
        $validation=array(
                'jobfunction'=>array(
                    'data'=>$data['user_jobfunction_id'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Job Function'
                ));
            if(empty($data['user_jobprofile1_id']) && empty($data['profile_reference_user_id'])){
                return false;
            }elseif(!empty($data['user_jobprofile1_id']) && empty($data['profile_reference_user_id'])){
                
                $validation['jobprofile']=array(
                    'data'=>$data['user_jobprofile1_id'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Job Profile 1'
                );
            }elseif(empty($data['user_jobprofile1_id']) && !empty($data['profile_reference_user_id'])){
                
                $validation['profilereference']=array(
                    'data'=>$data['profile_reference_user_id'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Profile Reference'
                );
            }else{
                $validation['jobprofile']=array(
                    'data'=>$data['user_jobprofile1_id'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Job Profile 1'
                );
                $validation['profilereference']=array(
                    'data'=>$data['profile_reference_user_id'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Profile Reference'
                );
            }
            
              
           
            if ($this->Validform->validateArray($validation)){
                return true;
            }else{
                $this->msg.=$this->Validform->getMsg();
                return false;
            }
      
    }
    function _validateTool(){
        $data=$this->data['Dmdform'];
                
            $validation=array(
                'tool'=>array(
                    'data'=>$data['tool_id0'],
                    'rules'=>array('notEmpty'),
                    'fieldName'=>'Tool 1'
                ),                
            );
            if ($this->Validform->validateArray($validation)){
                return true;
            }else{
                $this->msg.=$this->Validform->getMsg();
                return false;
            }
      
    }

    //find company
    function findCompany($formId=null,$save=false){    
            $this->data['Dmdform']['is_new_company']=0;
       // $this->data['Dmdform']['company']=0;
        if($save){$this->saveData($formId,$this->data);}
        $formData['countries']= $this->Dmdform->Company->Country->find('list');
         if(isset($this->data['Dmdform']['company_country'])){
             $countryId=$this->data['Dmdform']['company_country'];
             $t=$this->Dmdform->Company->find('all',array(
                'recursive'=>-1,
                 'conditions'=>array('country_id'=>$countryId),
                'fields'=>array('DISTINCT title'),
            ));
            $r=Set::extract($t,"/Company/title");
            $formData['c_companies']=$r;
            

        $companyTitle=$this->data['Dmdform']['company_title'];
             $t=$this->Dmdform->Company->find('all',array(
                'recursive'=>-1,
                 'conditions'=>array('Company.title'=>$companyTitle),
                'fields'=>array('DISTINCT site'),
            ));
            $r=Set::extract($t,"/Company/site");
            $formData['c_sites']=$r;
        

       $site=$this->data['Dmdform']['company_site'];
            $t=$this->Dmdform->Company->find('all',array(
                'recursive'=>-1,
                 'conditions'=>array('Company.site'=>$site),
                'fields'=>array('DISTINCT department'),
            ));
            $r=Set::extract($t,"/Company/department");
            $formData['c_departments']=$r;
         }

        $this->set('formData',$formData); return $formData;
    }
    
    
    //*
   
	function index() {
            $this->set('active',3);
		$this->Dmdform->recursive = 0;
		$this->set('dmdforms', $this->paginate());
	}

	function view($id = null) {
            $this->set('active',3);
		if (!$id) {
			$this->Session->setFlash(__('Invalid dmdform', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('dmdform', $this->Dmdform->read(null, $id));
	}

	function add() {
            $this->set('active',3);
		if (!empty($this->data)) {
			$this->Dmdform->create();
			if ($this->Dmdform->save($this->data)) {
				$this->Session->setFlash(__('The dmdform has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The dmdform could not be saved. Please, try again.', true));
			}
		}
		$dmdformtypes = $this->Dmdform->Dmdformtype->find('list');
		$demanders = $this->Dmdform->Demander->find('list');
		$users = $this->Dmdform->User->find('list');
		$userHrreferents = $this->Dmdform->UserHrreferent->find('list');
		$backups = $this->Dmdform->Backup->find('list');
		$managers = $this->Dmdform->Manager->find('list');
		$profileReferences = $this->Dmdform->ProfileReference->find('list');
		$userStatuses = $this->Dmdform->UserStatus->find('list');
		$jobprofile1s = $this->Dmdform->Jobprofile1->find('list');
		$jobprofile2s = $this->Dmdform->Jobprofile2->find('list');
		$jobprofile3s = $this->Dmdform->Jobprofile3->find('list');
		$jobfunctions = $this->Dmdform->Jobfunction->find('list');
		$companies = $this->Dmdform->Company->find('list');
		$this->set(compact('dmdformtypes', 'demanders', 'users', 'userHrreferents', 'backups', 'managers', 'profileReferences', 'userStatuses', 'jobprofile1s', 'jobprofile2s', 'jobprofile3s', 'jobfunctions', 'companies'));
	}

	function edit($id = null) {
            $this->set('active',3);
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid dmdform', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Dmdform->save($this->data)) {
				$this->Session->setFlash(__('The dmdform has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The dmdform could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Dmdform->read(null, $id);
		}
		$dmdformtypes = $this->Dmdform->Dmdformtype->find('list');
		$demanders = $this->Dmdform->Demander->find('list');
		$users = $this->Dmdform->User->find('list');
		$userHrreferents = $this->Dmdform->UserHrreferent->find('list');
		$backups = $this->Dmdform->Backup->find('list');
		$managers = $this->Dmdform->Manager->find('list');
		$profileReferences = $this->Dmdform->ProfileReference->find('list');
		$userStatuses = $this->Dmdform->UserStatus->find('list');
		$jobprofile1s = $this->Dmdform->Jobprofile1->find('list');
		$jobprofile2s = $this->Dmdform->Jobprofile2->find('list');
		$jobprofile3s = $this->Dmdform->Jobprofile3->find('list');
		$jobfunctions = $this->Dmdform->Jobfunction->find('list');
		$companies = $this->Dmdform->Company->find('list');
		$this->set(compact('dmdformtypes', 'demanders', 'users', 'userHrreferents', 'backups', 'managers', 'profileReferences', 'userStatuses', 'jobprofile1s', 'jobprofile2s', 'jobprofile3s', 'jobfunctions', 'companies'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for dmdform', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Dmdform->delete($id)) {
			$this->Session->setFlash(__('Dmdform deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Dmdform was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    //*/
}
?>
